package com.andy.nature.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.andy.common.extend.setOnIMClickListener
import com.andy.nature.R
import com.andy.nature.databinding.TitleBarViewBinding

/**
 * Author:Andy
 * Date:2023/4/6 17:15
 * Description:TitleBarView
 */
class TitleBarView : ConstraintLayout {
    private lateinit var mBinding: TitleBarViewBinding
    private var clickLeftCallBack: (() -> Unit)? = null
    private var clickCenterCallBack: (() -> Unit)? = null
    private var clickRightCallBack: (() -> Unit)? = null

    constructor(context: Context) : super(context) {
        TitleBarView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(attrs)
    }

    fun setClickLeftCallBack(clickLeftCallBack: (() -> Unit)?) {
        this.clickLeftCallBack = clickLeftCallBack
    }

    fun setClickCenterCallBack(clickCenterCallBack: (() -> Unit)?) {
        this.clickCenterCallBack = clickCenterCallBack
    }

    fun setClickRightCallBack(clickRightCallBack: (() -> Unit)?) {
        this.clickRightCallBack = clickRightCallBack
    }

    private fun initView(attrs: AttributeSet?) {
        mBinding = TitleBarViewBinding.inflate(LayoutInflater.from(context), this)
        setBackgroundResource(com.andy.base.R.color.color_317CFF)
        attrs.let {
            val typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarViewStyle)
            val drawableLeft = typeArray.getResourceId(R.styleable.TitleBarViewStyle_TitleBarDrawableLeft, 0)
            val title = typeArray.getString(R.styleable.TitleBarViewStyle_TitleBarTitle)
            val drawableRight = typeArray.getResourceId(R.styleable.TitleBarViewStyle_TitleBarDrawableRight, 0)
            val isShowLeft = typeArray.getBoolean(R.styleable.TitleBarViewStyle_TitleBarIsShowLeft, true)
            val isShowTitle = typeArray.getBoolean(R.styleable.TitleBarViewStyle_TitleBarIsShowTitle, true)
            val isShowRight = typeArray.getBoolean(R.styleable.TitleBarViewStyle_TitleBarIsShowRight, false)
            typeArray.recycle()
            if (drawableLeft != 0) {
                mBinding.ivLeft.setImageResource(drawableLeft)
            }
            if (!title.isNullOrEmpty()) {
                mBinding.tvTitle.text = title
            }
            if (drawableRight != 0) {
                mBinding.ivRight.setImageResource(drawableRight)
            }
            mBinding.ivLeft.isVisible = isShowLeft
            mBinding.tvTitle.isVisible = isShowTitle
            mBinding.ivRight.isVisible = isShowRight
        }
        mBinding.apply {
            ivLeft.setOnIMClickListener {
                clickLeftCallBack?.invoke()
            }
            tvTitle.setOnIMClickListener {
                clickCenterCallBack?.invoke()
            }
            ivRight.setOnIMClickListener {
                clickRightCallBack?.invoke()
            }
        }
    }

    fun setDrawableLeft(resId: Int) {
        if (resId != 0) {
            mBinding.ivLeft.setImageResource(resId)
        }
    }

    fun setTitle(title: String) {
        if (!title.isNullOrEmpty()) {
            mBinding.tvTitle.text = title
        }
    }

    fun setDrawableRight(resId: Int) {
        if (resId != 0) {
            mBinding.ivRight.setImageResource(resId)
        }
    }

    fun showLeft(isVisible: Boolean) {
        mBinding.ivLeft.isVisible = isVisible
    }

    fun showTitle(isVisible: Boolean) {
        mBinding.tvTitle.isVisible = isVisible
    }

    fun showRight(isVisible: Boolean) {
        mBinding.ivRight.isVisible = isVisible
    }

}